<!DOCTYPE html>
            
<HTML>
<HEAD>
<meta name="booktitle" content="Developing Applications With Objective Caml" >
 <meta charset="ISO-8859-1"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<META name="GENERATOR" content="hevea 1.05-7 of 2000-02-24">
<META NAME="Author" CONTENT="Christian.Queinnec@lip6.fr">
<LINK rel=stylesheet type="text/css" href="videoc-ocda.css">
<script language="JavaScript" src="videoc.js"><!--
//--></script>
<TITLE>
 Presenting part I
</TITLE>
</HEAD>
<BODY class="regularBody">
<A HREF="book-ora010.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<A HREF="book-ora013.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2> Presenting part I</H2>
The first part of this book is a complete introduction to the core of the
Objective CAML language, in particular the expression evaluation mechanism,
static typing and the data memory model.<BR>
<BR>
An expression is the description of a computation. Evaluation of an
expression returns a value at the end of the computation. The execution of
an Objective CAML program corresponds to the computation of an expression.
Functions, program execution control structures, even conditions or loops,
are themselves also expressions.<BR>
<BR>
Static typing guarantees that the computation of an expression cannot cause
a run-time type error. In fact, application of a function to some
arguments (or actual parameters) isn't accepted unless they all have types
compatible with the formal parameters indicated in the definition of the
function. Furthermore, the Objective CAML language has type infererence: the
compiler automatically determines the most general type of an expression. <BR>
<BR>
Finally a minimal knowledge of the representation of data is indispensable
to the programmer in order to master the effects of physical modifications
to the data.<BR>
<BR>

<H4>Outline</H4>
Chapter <A HREF="index.html#chap-PF">2</A> contains a complete presentation of the purely
functional part of the language and the constraints due to static typing.
The notion of expression evaluation is illustrated there at length. The
following control structures are detailed: conditional, function
application and pattern matching. The differences between the type and the
domain of a function are discussed in order to introduce the exception
mechanism. This feature of the language goes beyond the functional context
and allows management of computational breakdowns.<BR>
<BR>
Chapter <A HREF="index.html#chap-PI">3</A> exhibits the imperative style. The constructions
there are closer to classic languages. Associative control structures such
as sequence and iteration are presented there, as well as mutable data
structures. The interaction between physical modifications and sharing of
data is then detailed. Type inference is described there in the context of
these new constructions.<BR>
<BR>
Chapter <A HREF="index.html#chap-C-Styles">4</A> compares the two preceding styles and
especially presents different mixed styles. This mixture supports in
particular the construction of lazy data structures, including mutable
ones.<BR>
<BR>
Chapter <A HREF="index.html#chap-PG">5</A> demonstrates the use of the <TT>Graphics</TT>
library included in the language distribution. The basic notions of
graphics programming are exhibited there and immediately put into practice.
There's even something about GUI construction thanks to the minimal event
control provided by this library.<BR>
<BR>
These first four chapters are illustrated by a complete example, the
implementation <BR>of a calculator, which evolves from chapter to chapter.<BR>
<BR>
Chapter <A HREF="index.html#chap-PART1-Applications">6</A> presents three complete
applications: a little database, a mini-BASIC interpreter and the game
Minesweeper. The first two examples are constructed mainly in a functional
style, while the third is done in an imperative style.<BR>
<BR>

<H4>The rudiments of syntax</H4>
Before beginning we indicate the first elements of the syntax of the
language. A program is a sequence of phrases in the language. A phrase is
a complete, directly executable syntactic element (an expression, a
declaration). A phrase is terminated with a double semi-colon
(;;). There are three different types of declarations which are
each marked with a different keyword:
<DIV ALIGN=center>
<TABLE CELLSPACING=2 CELLPADDING=0>
<TR><TD  ALIGN=left NOWRAP>value declaration</TD>
<TD  ALIGN=left NOWRAP>:</TD>
<TD  ALIGN=left NOWRAP><B>let</B></TD>
</TR>
<TR><TD  ALIGN=left NOWRAP>exception declaration</TD>
<TD  ALIGN=left NOWRAP>:</TD>
<TD  ALIGN=left NOWRAP><B>exception</B></TD>
</TR>
<TR><TD  ALIGN=left NOWRAP>type declaration</TD>
<TD  ALIGN=left NOWRAP>:</TD>
<TD  ALIGN=left NOWRAP><B>type</B></TD>
</TR></TABLE>
</DIV>
All the examples given in this part are to be input into the interactive
toplevel of the language.<BR>
<BR>
Here's a first (little) Objective CAML program, to be entered into the toplevel,
whose prompt is the pound character (<CODE>#</CODE>),
in which a function fact
computing the factorial of a natural number, and
its application to a natural number <CODE>8</CODE>, are defined.


<PRE><BR># <B>let</B><CODE> </CODE><B>rec</B><CODE> </CODE>fact<CODE> </CODE>n<CODE> </CODE><CODE>=</CODE><CODE> </CODE><B>if</B><CODE> </CODE>n<CODE> </CODE><CODE>&lt;</CODE><CODE> </CODE><CODE>2</CODE><CODE> </CODE><B>then</B><CODE> </CODE><CODE>1</CODE><CODE> </CODE><B>else</B><CODE> </CODE>n<CODE> </CODE><CODE>*</CODE><CODE> </CODE>fact<TT>(</TT>n<CODE>-</CODE><CODE>1</CODE><TT>)</TT><CODE> </CODE>;;<BR><CODE>val fact : int -&gt; int = &lt;fun&gt;</CODE><BR># fact<CODE> </CODE><CODE>8</CODE><CODE> </CODE>;;<BR><CODE>- : int = 40320</CODE><BR>

</PRE>

This program consists of two <EM>phrases</EM>. The first is the declaration of
a function value and the second is an expression. One sees that the
toplevel prints out three pieces of information which are: the name being
declared, or a dash (<CODE>-</CODE>) in the case of an expression; the inferred
type; and the return value. In the case of a function value, the system
prints <CODE>&lt;fun&gt;</CODE>.<BR>
<BR>
The following example demonstrates the manipulation of functions as values
in the language. There we first of all define the function succ
which calculates the successor of an integer, then the function
compose which composes two functions. The latter will be
applied to fact and succ.<BR>
<BR>


<PRE><BR># <B>let</B><CODE> </CODE>succ<CODE> </CODE>x<CODE> </CODE><CODE>=</CODE><CODE> </CODE>x<CODE>+</CODE><CODE>1</CODE><CODE> </CODE>;;<BR><CODE>val succ : int -&gt; int = &lt;fun&gt;</CODE><BR># <B>let</B><CODE> </CODE>compose<CODE> </CODE>f<CODE> </CODE>g<CODE> </CODE>x<CODE> </CODE><CODE>=</CODE><CODE> </CODE>f<TT>(</TT>g<CODE> </CODE>x<TT>)</TT><CODE> </CODE><CODE> </CODE>;;<BR><CODE>val compose : ('a -&gt; 'b) -&gt; ('c -&gt; 'a) -&gt; 'c -&gt; 'b = &lt;fun&gt;</CODE><BR># compose<CODE> </CODE>fact<CODE> </CODE>succ<CODE> </CODE><CODE>8</CODE><CODE> </CODE>;;<BR><CODE>- : int = 362880</CODE><BR>

</PRE>
 
This last call carries out the computation fact<TT>(</TT>succ<CODE> </CODE><CODE>8</CODE><TT>)</TT> and
returns the expected result. Let us note that the functions
fact and succ are passed as parameters to compose in
the same way as the natural number <CODE>8</CODE>.
<BR>
<BR>
<BR>
<BR>
<HR>
<A HREF="book-ora010.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<A HREF="book-ora013.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
